package com.eightgroups.tces.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.eightgroups.common.result.JsonResult;
import com.eightgroups.common.utils.StringUtil;
import com.eightgroups.tces.entity.*;
import com.eightgroups.tces.service.IClazzService;
import com.eightgroups.tces.service.ICoursePlanService;
import com.eightgroups.tces.service.ICourseService;
import com.eightgroups.tces.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import com.eightgroups.common.controller.BaseController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 开课信息 前端控制器
 * </p>
 *
 * @author zzc2016.cn
 * @since 2017-11-27
 */
@Controller
@RequestMapping("/admin/coursePlan")
public class CoursePlanController extends BaseController {
    @Autowired
    private ICoursePlanService iCoursePlanService;
    @Autowired
    private IClazzService iClazzService;
    @Autowired
    private ICourseService iCourseService;
    @Autowired
    private ITeacherService iTeacherService;
    /**
     * 开课信息列表页面
     * @return
     */
    @GetMapping("/list")
    public String list(Model model){
        EntityWrapper<Clazz> clazzEntityWrapper = new EntityWrapper<>();
        clazzEntityWrapper.eq("status",1);
        List<Clazz> clazzes = iClazzService.selectList(clazzEntityWrapper);
        EntityWrapper<Course> courseEntityWrapper = new EntityWrapper<>();
        courseEntityWrapper.eq("status",1);
        List<Course> courses = iCourseService.selectList(courseEntityWrapper);
        EntityWrapper<Teacher> teacherEntityWrapper = new EntityWrapper<>();
        teacherEntityWrapper.eq("status",1);
        List<Teacher> teachers = iTeacherService.selectList(teacherEntityWrapper);
        model.addAttribute("clazzes",clazzes);
        model.addAttribute("courses",courses);
        model.addAttribute("teachers",teachers);
        return "admin/courseplan/list";
    }
    /**
     * 获取系部列表
     * @param pageNumber 当前页
     * @param pageSize 每页显示条数
     * @param searchText 搜索名称
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public Map<String,Object> getList(int pageNumber, int pageSize, String searchText){
        Map<String,Object> result = new HashMap<String,Object>();
        Page<CoursePlanVo> coursePlanVoPage = new Page<>(pageNumber,pageSize);
        EntityWrapper<CoursePlanVo> entityWrapper = new EntityWrapper<>();
        if(StringUtil.isNotEmpty(searchText)){
            entityWrapper.like("cname",searchText);
        }
        Page<CoursePlanVo> page = iCoursePlanService.selectPageVo(coursePlanVoPage,entityWrapper);
        result.put("total", page.getTotal());
        result.put("rows", page.getRecords());
        return result;
    }
    /**
     * 添加开课信息
     * @param coursePlan
     * @return
     */
    @RequestMapping("/addCoursePlan")
    @ResponseBody
    public JsonResult addCoursePlan(CoursePlan coursePlan){
        return iCoursePlanService.insert(coursePlan)?renderSuccess("添加成功"):renderError("添加失败");
    }
    /**
     * 修改课程
     * @param coursePlan
     * @return
     */
    @RequestMapping("/updateCoursePlan")
    @ResponseBody
    public JsonResult updateCoursePlan(CoursePlan coursePlan){
        return iCoursePlanService.updateById(coursePlan)?renderSuccess("修改成功"):renderError("修改失败");
    }

    /**
     * 删除课程
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(@RequestParam(value = "id", required = false) String id) {
        if (StringUtil.isEmpty(id)) {
            return renderError("请选择数据");
        }
        String[] split = id.split(",");
        List<String> ids = new ArrayList<>();
        for (String item:split) {
            ids.add(item);
        }
        return iCoursePlanService.deleteBatchIds(ids)? renderSuccess("删除成功") : renderError("删除失败");
    }
}
